package com.ht.springcloud.filters;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class MyPostFilter01 extends ZuulFilter {
    @Override
    public String filterType() {
        //返回过滤器的类型
        return FilterConstants.POST_TYPE;
    }

    @Override
    public int filterOrder() {
        //返回过滤器顺序，在type相同的情况下，数字越小，优先级越高
        return FilterConstants.PRE_DECORATION_FILTER_ORDER+1;
    }
//当前过滤器是否开启，如果返回false，则不开启，就不会执行对应的操作
    @Override
    public boolean shouldFilter() {
        return true;
    }
    //如果过滤器开启了，则会执行当前的方法
    @Override
    public Object run() throws ZuulException {
        RequestContext requestContext = RequestContext.getCurrentContext();
        HttpServletRequest request = requestContext.getRequest();
        HttpServletResponse response = requestContext.getResponse();
        String requestURI = request.getRequestURI();
        System.err.println(requestURI);
        response.setContentType("text/html;charset=utf-8");
        requestContext.put("laowang","开炮");
        System.err.println("MyPostFilter01执行了");
        return null;
    }
}
